package com.example.com.hupu.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.example.com.hupu.R;
import com.example.com.hupu.adapter.NBANewsAdapter;
import com.example.com.hupu.bean.NBANewItem;
import com.example.com.hupu.bean.NBANews;
import com.example.com.hupu.bean.NewsItem;
import com.example.com.hupu.net.RetrofitNewsUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * NBA模块的Fragment
 */
public class NBAFragment extends Fragment {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    Unbinder unbinder;
    private String mStringIds;
    private List<NewsItem> mNewsItems;

    private NBANewsAdapter mAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_nba, null);
        unbinder = ButterKnife.bind(this, view);

        RetrofitNewsUtils.getApiService().getNBANewsID("banner").
                enqueue(new Callback<NBANews>() {
                    @Override
                    public void onResponse(Call<NBANews> call, Response<NBANews> response) {
                        getNBANewsId(response);
                        getNBANews();
                    }

                    @Override
                    public void onFailure(Call<NBANews> call, Throwable t) {

                    }
                });

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        return view;
    }

    private void getNBANews() {
        RetrofitNewsUtils.getApiService().getNBANews("banner", mStringIds)
                .enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        mNewsItems = new ArrayList<>();
                        try {
                            String json = response.body().string();
                            JSONObject jsonObject = new JSONObject(json);
                            JSONObject data = jsonObject.getJSONObject("data");
                            Iterator<String> keys = data.keys();
                            while (keys.hasNext()) {
                                String next = keys.next();
                                JSONObject itemJO = data.getJSONObject(next);
                                NewsItem newsItem = JSON.parseObject(itemJO.toString(), NewsItem.class);
                                mNewsItems.add(newsItem);
                            }
                            mAdapter = new NBANewsAdapter(mNewsItems);
                            recyclerView.setAdapter(mAdapter);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {

                    }
                });
    }

    private void getNBANewsId(Response<NBANews> response) {
        NBANews nbaNews = response.body();
        if (nbaNews != null) {
            List<NBANewItem> items = nbaNews.data;
            NBANewItem lastItem = items.get(items.size() - 1);
            StringBuilder builder = new StringBuilder();
            for (NBANewItem item : items) {
                builder.append(item.id);
                if (item != lastItem) {
                    builder.append(",");
                }
            }
            mStringIds = builder.toString();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
